/*******************************************************************************
 * 版权：北京润尼尔网络科技有限公司，保留所有版权<br>
 * 版本：v1.0.0<br>
 * 文件：DeviceBoxFigure.js<br>
 * 日期：2016年10月21日<br>
 * 作者: 王志远<br>
 * 功能：器件盒子图元<br>
 * 
 * 修改记录：<br>
 * 日期 描述 更新功能<br>
 ******************************************************************************/
window.makeNamespace(window,"Rainier.UI.V2");

/**
 * 类型名称：Rainier.UI.V2.DeviceBoxFigure<br>
 * 类型功能：器件盒子图元<br>
 * 接口函数：<br>
 */
Rainier.UI.V2.DeviceBoxFigure = Rainier.UI.V2.ComponentFigure.extend({
	
	/**
	 * 函数名称：Rainier.UI.V2.DeviceBoxFigure.init<br>
	 * 函数功能：构造函数<br>
	 * 参数输入：property<br>
	 * 异常信息：<br>
	 */
	init:function(property){
		var dummy = null;
		
		//复制属性对象
		if(property){
			var width = property.uiProperty.width;
			var height = property.uiProperty.height;
			dummy = $.extend({},{width:width,height:height});
		}
		
		if(dummy){ //修改画笔为0（不显示边框）
			dummy.stroke = 0;	
			dummy.alpha  = 0.02;
		}else{     //如果属性对象为null或未定义，则创建默认对象
			dummy = {
				width : 60,
				height: 60,
				stroke: 0,		//画笔为0
				alpha : 0.02,	//几乎完全透明
				angle : 0		//角度为0
			};
		}
		var imageProperty = {};
		imageProperty.width = property.imageProperty.width;
		imageProperty.height= property.imageProperty.height;
		imageProperty.svg   = property.imageProperty.svg;
		this._super(imageProperty);
		
		this.uiProperty = property.uiProperty;
		
		this.installEditPolicy(new draw2d.policy.figure.AntSelectionFeedbackPolicy());
		
		this.portNumberVisible = false;	//是否显示端口编号
		this.portSymbolVisible = false;	//是否显示端口符号
		this.labelVisible	   = false;	//是否显示标签
		this.parameterVisible  = false;	//是否显示参数值
		this.devNameVisible	   = false;	//是否显示器件名称
		this.devEngNameVisible	   = false;	//是否显示器件英文名称
		
		this.bodyImageFigure 	= null; //器件图元
		this.parameterFigure	= null;	//器件参数图元
		this.devNameFigure		= null; //器件名称图元
		this.symbolLabelFigure	= null; //符号图元
		this.portSymbolLabels   = new draw2d.util.ArrayList();	//符号引脚图元列表
		this.portNumberLabels	= new draw2d.util.ArrayList();	//数字引脚图元列表
		this.subImageList		= new draw2d.util.ArrayList();	//子图元列表
		this.subTextList		= new draw2d.util.ArrayList();	//子文本列表
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.DeviceBoxFigure.createSet<br>
	 * 函数功能：创建背景图片<br>
	 * 参数输入：无<br>
	 * 参数输出：无<br>
	 * 函数返回：set:canvas.paper.set<br>
	 * 异常信息：无<br>
	 */
	createSet: function() {
	      var bgImage = this._super();
	      var set = this.canvas.paper.set();
	      set.push(bgImage);
	      
	      this.createSubImage(this.uiProperty);
	      this.subImageList.each(function(index,subImage){
	    	  set.push(subImage.image);
	      })
	      return set;
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.DeviceBoxFigure.updateDynamicSet<br>
	 * 函数功能：更新动态图片<br>
	 * 参数输入：无<br>
	 * 参数输出：无<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	updateDynamicSet : function(){
		var _this = this;
		var rotation = this.getRotationAngle();
		this.subImageList.each(function(index,subImage){
			if(!subImage.visible){
			  subImage.image.hide();
			}
			else{
			  subImage.image.show();
			}
		});
		this.subTextList.each(function(index,subText){
			subText.label.setText(subText.text);
			if(rotation > 0 ){
				var command = new draw2d.command.CommandRotate(subText.label,rotation);
				command.execute();				
			}
		})
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.DeviceBoxFigure.createSubImage<br>
	 * 函数功能：创建子图元，适用于动态元件<br>
	 * 参数输入：<br>
	 * 参数输出：<br>
	 * 函数返回：<br>
	 * 异常信息：<br>
	 */
	createSubImage : function(property){	
		var _this = this;
		var subImageList 	= property.subImageList;
		var subTextList		= property.subTextList;
		var imageOnString	= property.imageOnString;
		if(imageOnString && subImageList && subImageList.length > 0){
			$.each(subImageList,function(index,subImage){
				var imageObj = {};
				var imageIndex  = subImage.imageIndex;
				var imageString = imageOnString[imageIndex].image;
				var imagePos	= subImage.position;
				var image       = _this.canvas.paper.image(imageString,imagePos.x,imagePos.y,imagePos.width,imagePos.height);
				if(image){
					imageObj.image 		= image;
					_this.subImageList.add($.extend(imageObj,subImage));
				}
			})
		}
		if(subTextList && subTextList.length > 0){
			$.each(subTextList,function(index,subText){
				var textObj = {};
				var label	= new draw2d.shape.basic.Label($.extend({text:'',stroke:0,fontColor:'#ffffff'},subText));
        		label.setPadding(0);
				var locator	= new Rainier.UI.V2.XYAbsFigureLocator(subText.position.x,subText.position.y - 3);
				_this.add(label,locator);

				textObj.label 		= label;
				_this.subTextList.add($.extend(textObj,subText));
			})
		}
	},

	/**
	 * 函数名称：Rainier.UI.V2.DeviceBoxFigure.setRotationAngle<br>
	 * 函数功能：图元选择<br>
	 * 参数输入：<br>
	 * angle:Number角度值
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	setRotationAngle : function(angle){
		angle = angle % 360;
        if(angle > 0 && angle < 1e-6){
            angle = 0;
        }
//		if(angle === 90 || angle ===270){
//        	angle += 1e-9;
//		}
		//var shape = this.shape;
		this._super(angle);
        this.children.each(function(i,e){
            var figure = e.figure;
            if(!(figure instanceof Rainier.UI.V2.DeviceSymbolLabelFigure)){
            	figure.setRotationAngle(angle);            	
            }
        });
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.DeviceBoxFigure.isPortNumberVisible<br>
	 * 函数功能：获取端口编号显示状态<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	isPortNumberVisible : function(){
		return this.portNumberVisible;
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.DeviceBoxFigure.isPortSymbolVisible<br>
	 * 函数功能：获取端口名称显示状态<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	isPortSymbolVisible : function(){
		return this.portSymbolVisible;
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.DeviceBoxFigure.isLabelVisible<br>
	 * 函数功能：查询标签是否显示<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	isLabelVisible : function(){
		return this.labelVisible;
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.DeviceBoxFigure.isParameterVisible<br>
	 * 函数功能：查询参数是否显示<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	isParameterVisible : function(){
		return this.parameterVisible;
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.DeviceBoxFigure.isDeviceNameVisiable<br>
	 * 函数功能：查询器件名称是否显示<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	isDeviceNameVisiable : function(){
		return this.devNameVisible;
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.DeviceBoxFigure.isDeviceEngNameVisiable<br>
	 * 函数功能：查询器件英文名称是否显示<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	isDeviceEngNameVisiable : function(){
		return this.devEngNameVisible;
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.DeviceBoxFigure.unshowPortNumber<br>
	 * 函数功能：不显示端口编号<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	unshowPortNumber : function(){
		this.portNumberVisible = false;
		this.portNumberLabels.each(function(i,figure){
			figure.setVisible(false); 
		});
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.DeviceBoxFigure.unshowPortNumber<br>
	 * 函数功能：不显示端口编号<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	showPortNumber : function(){
		this.portNumberVisible = true;
		this.portNumberLabels.each(function(i,figure){
			figure.setVisible(true); 
		});
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.DeviceBoxFigure.unshowPortSymbol<br>
	 * 函数功能：不显示端口编号<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	unshowPortSymbol : function(){
		this.portSymbolVisible = false;
		this.portSymbolLabels.each(function(i,figure){
			figure.setVisible(false); 
		});
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.DeviceBoxFigure.showPortSymbol<br>
	 * 函数功能：不显示端口编号<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	showPortSymbol : function(){
		this.portSymbolVisible = true;
		this.portSymbolLabels.each(function(i,figure){
			figure.setVisible(true); 
		});
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.DeviceBoxFigure.showLabel<br>
	 * 函数功能：显示标签<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	showLabel : function(){
		this.labelVisible = false;
		if(this.symbolLabelFigure){
			this.symbolLabelFigure.setVisible(true);
		}
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.DeviceBoxFigure.unshowLabel<br>
	 * 函数功能：隐藏标签<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	unshowLabel : function(){
		this.labelVisible = false;
		if(this.symbolLabelFigure){
			this.symbolLabelFigure.setVisible(false);
		}
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.DeviceBoxFigure.showParameter<br>
	 * 函数功能：隐藏标签<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	showParameter : function(){
		this.parameterVisible = true;
		if(this.parameterFigure){
			this.parameterFigure.setVisible(true);
		}
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.DeviceBoxFigure.unshowParameter<br>
	 * 函数功能：隐藏标签<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	unshowParameter : function(){
		this.parameterVisible = false;
		if(this.parameterFigure){
			this.parameterFigure.setVisible(false);
		}
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.DeviceBoxFigure.showDeviceName<br>
	 * 函数功能：显示设备名称<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	showDeviceName : function(){
		this.devNameVisible = true;
		if(this.devNameFigure){
			this.devNameFigure.setVisible(true);
		}
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.DeviceBoxFigure.showDeviceName<br>
	 * 函数功能：隐藏设备名称<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	unshowDeviceName : function(){
		this.devNameVisible = false;
		if(this.devNameFigure){
			this.devNameFigure.setVisible(false);
		}
	},
	
	
	/**
	 * 函数名称：Rainier.UI.V2.DeviceBoxFigure.showDeviceEngName<br>
	 * 函数功能：显示设备英文名称<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	showDeviceEngName : function(){
		this.devEngNameVisible = true;
		if(this.devEngNameFigure){
			this.devEngNameFigure.setVisible(true);
		}
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.DeviceBoxFigure.showDeviceEngName<br>
	 * 函数功能：隐藏设备英文名称<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	unshowDeviceEngName : function(){
		this.devEngNameVisible = false;
		if(this.devEngNameFigure){
			this.devEngNameFigure.setVisible(false);
		}
	},
	
	
	/**
	 * 函数名称：Rainier.UI.V2.DeviceBoxFigure.setBodyImageFigure<br>
	 * 函数功能：设置svg图片<br>
	 * 参数输入：<br>
	 * property:属性对象<br>
	 * sLocator:定位器构造函数<br>
	 * x:Number<br>
	 * y:Number<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	setBodyImageFigure : function(property,sLocator,x,y){
		var locator = null;
		if(!sLocator || sLocator === 'default'){
			locator = new Rainier.UI.V2.XYAbsFigureLocator(x,y);
		}else{
			locator = eavl('new ' + sLocator + '();');
			locator.x = x;
			locator.y = y;
		}
		//this.bodyImageFigure = new Rainier.UI.V2.DeviceBodyImageFigure(property);
		this.bodyImageFigure = new Rainier.UI.V2.ComponentFigure(property);
		this.add(this.bodyImageFigure,locator);
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.DeviceBoxFigure.setSymbolLabelFigure<br>
	 * 函数功能：设置Label<br>
	 * 参数输入：<br>
	 * property:属性对象<br>
	 * sLocator:定位器构造函数<br>
	 * x:Number<br>
	 * y:Number<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	setSymbolLabelFigure : function(property,sLocator,x,y){
		var locator = null;
		if(!sLocator || sLocator === 'default'){
			locator = new Rainier.UI.V2.XYAbsFigureLocator(x,y);
		}else{
			locator = eval('new ' + sLocator + '();');
			locator.offsetX = x;
			locator.offsetY = y;
		}
		property.stroke = 0;
		this.symbolLabelFigure = new Rainier.UI.V2.DeviceSymbolLabelFigure(property,locator);
		this.add(this.symbolLabelFigure,locator);
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.DeviceBoxFigure.setParameterFigure<br>
	 * 函数功能：设置svg图片<br>
	 * 参数输入：<br>
	 * property:属性对象<br>
	 * sLocator:定位器构造函数<br>
	 * x:Number<br>
	 * y:Number<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	setParameterFigure : function(property,sLocator,x,y){
		var locator = null;
		if(!sLocator || sLocator === 'default'){
			locator = new Rainier.UI.V2.XYAbsFigureLocator(x,y);
		}else{
			locator = eval('new ' + sLocator + '();');
			locator.offsetX = x;
			locator.offsetY = y;
		}
		property.stroke = 0;
		this.parameterFigure = new Rainier.UI.V2.DeviceSymbolLabelFigure(property,locator);
		this.add(this.parameterFigure,locator);
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.DeviceBoxFigure.setParameterFigure<br>
	 * 函数功能：设置设备名称图元<br>
	 * 参数输入：<br>
	 * property:属性对象<br>
	 * sLocator:定位器构造函数<br>
	 * x:Number<br>
	 * y:Number<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	setDeviceNameFigure : function(property,sLocator,x,y){
		var locator = null;
		if(!sLocator || sLocator === 'default'){
			locator = new Rainier.UI.V2.XYAbsFigureLocator(x,y);
		}else{
			locator 			  = eval('new ' + sLocator + '();');
			locator.offsetX = x;
			locator.offsetY = y;
		}
		property.stroke = 0;
		this.devNameFigure 	  = new Rainier.UI.V2.DeviceSymbolLabelFigure(property,locator);
		this.add(this.devNameFigure,locator);
	},
	/**
	 * 函数名称：Rainier.UI.V2.DeviceBoxFigure.setParameterFigure<br>
	 * 函数功能：设置设备英文名称图元<br>
	 * 参数输入：<br>
	 * property:属性对象<br>
	 * sLocator:定位器构造函数<br>
	 * x:Number<br>
	 * y:Number<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	setDeviceEngNameFigure : function(property,sLocator,x,y){
		var locator = null;
		if(!sLocator || sLocator === 'default'){
			locator = new Rainier.UI.V2.XYAbsFigureLocator(x,y);
		}else{
			locator 			  = eval('new ' + sLocator + '();');
			locator.offsetX = x;
			locator.offsetY = y;
		}
		property.stroke = 0;
		this.devEngNameFigure 	  = new Rainier.UI.V2.DeviceSymbolLabelFigure(property,locator);
		this.add(this.devEngNameFigure,locator);
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.DeviceBoxFigure.addPortSymbolLabelFigure<br>
	 * 函数功能：设置svg图片<br>
	 * 参数输入：<br>
	 * property:属性对象<br>
	 * sLocator:定位器构造函数<br>
	 * x:Number<br>
	 * y:Number<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	addPortSymbolLabelFigure : function(property,sLocator,x,y){
		var locator = null;
		if(!sLocator || sLocator === 'default'){
			locator = new Rainier.UI.V2.XYAbsFigureLocator(x,y);
		}else{
			locator = eval('new ' + sLocator + '();');
			locator.x = x;
			locator.y = y;
		}
		property.stroke = 0;
		property.visible = false;
		var figure = new Rainier.UI.V2.DevicePortSymbolLabelFigure(property,locator);
		if(property.symboltype){
			figure.addCssClass('text-'+property.symboltype);
		}
		this.portSymbolLabels.add(figure);
		this.add(figure,locator);
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.DeviceBoxFigure.addPortNumberLabelFigure<br>
	 * 函数功能：设置svg图片<br>
	 * 参数输入：<br>
	 * property:属性对象<br>
	 * sLocator:定位器构造函数<br>
	 * x:Number<br>
	 * y:Number<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	addPortNumberLabelFigure : function(property,sLocator,x,y){

		x = (!x)?-10:x;
		y = (!y)?y-5:y-10;
		
		var locator = null;
		if(!sLocator || sLocator === 'default'){
			locator = new Rainier.UI.V2.XYAbsFigureLocator(x,y);
		}else{
			locator = eval('new ' + sLocator + '();');
			locator.x = x;
			locator.y = y;
		}
		property.stroke = 0;
		property.visible = false;
		var figure = new Rainier.UI.V2.DevicePortNumberLabelFigure(property,locator);
		this.portNumberLabels.add(figure);
		this.add(figure,locator);
	},

	/**
     * @名称   repaint
     * @功能   绘制图元
     * @author 杨顺强
     * @data   2017-09-21
     * @param  {[type]}   attributes [description]
     * @return {[type]}              [description]
     */
    repaint : function(attributes){
        this._super(attributes);
    },
});
